package com.shop.listener;

import com.shop.service.ProductTypeService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

//注册listener
@WebListener
public class ProducutypeListner implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
//        从容器中获得 ProductTypeServiceImpl对象，因为spring容器也是在 添加在，servletcontext启动时添加
        //所以不能在spring容器中获得

        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext_*.xml");
        ProductTypeService productTypeService = (ProductTypeService) ac.getBean("productTypeServiceImpl");
        System.out.println(productTypeService.findAll() + "--------------->");


        //保存到全局作用域中
        servletContextEvent.getServletContext().setAttribute("typeList",productTypeService.findAll());
    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {

    }
}
